<?php
require_once dirname(__FILE__) . '/common.inc.php';

//if (isset($_COOKIE[LANG])) {
//  $language = $_COOKIE[LANG];
//} else {
//  $language = 'en';
//}
//$smarty->assign('lang', $language);
//
//// I18N support information here
//putenv("LANG=$language"); 
//setlocale(LC_ALL, $language);

// Set the text domain as 'messages'
//$domain = 'messages';
//bindtextdomain($domain, dirname(__FILE__). '/locale'); 
//bind_textdomain_codeset($domain, "utf-8");
//textdomain($domain);


if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}

$mdus = _fillModules(DbUtils::get(DbUtils::createPdoInst(), 'pz_module', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d);
$smarty->assign('mdus', $mdus);
$smarty->assign('folder', MiscUtils::getParam('folder', NULL));
$smarty->assign('arg', MiscUtils::getParam('arg', NULL));
$smarty->assign('mmenu', MiscUtils::getParam('mmenu', MiscUtils::getParam('arg', NULL)));

$template = MiscUtils::getParam('t', 'signin');
$options = MiscUtils::getParam('opts', json_encode(array()));
$path = MiscUtils::getParam('path', 'admin');
$base = MiscUtils::getParam('b', 'base');
if ($path == 'ecommerce') {
  $path = 'admin/ecommerce';
}

$smarty->assign('p', $path);
$smarty->assign('u', json_encode(array()));
$smarty->assign('d', json_encode(array()));
$smarty->assign('opts', json_encode(array()));
$smarty->assign('msg', json_encode(array()));

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[USER]);
  setcookie(USER, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('t', 'signin');
  $smarty->display("admin/baseblank.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[USER])) {
  if ($template == 'signin') {
    $template = 'pages';
  }

	$language = $_SESSION[USER]->language;
	$smarty->assign('lang', $language);
	
	// I18N support information here
	putenv("LANG=$language"); 
	setlocale(LC_ALL, $language);
  
	// Set the text domain as 'messages'
	$domain = 'messages';
	bindtextdomain($domain, dirname(__FILE__). '/locale'); 
	bind_textdomain_codeset($domain, "utf-8");
	textdomain($domain);
	
  $smarty->assign('profile', $_SESSION[USER]);
  $smarty->assign('t', "$template");
  $smarty->assign('u', json_encode($_SESSION[USER]));
  $smarty->assign('d', time());
  $smarty->assign('opts', urldecode($options));
  
  $message = array();
  loadPage($template, $message);
  $smarty->display("admin/$base.tpl");
  return;
} else {
  $smarty->assign('p', 'admin');
  $smarty->assign('t', 'signin');
  $smarty->display("admin/baseblank.tpl");
  return; /* SignIn Required */
}

function _fillModules($arr) {
  foreach ($arr as $tmp) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.module_track_id = :v1';
    $cond_vals->v = array(':v1' => $tmp->track_id);
    $tmp->menus = DbUtils::get(DbUtils::createPdoInst(), 'pz_module_menu', $cond_vals, 't.id', 'ASC', NULL, NULL, NULL)->d;
  }
  return $arr;
}

function loadPage($template, $message) {
  switch ($template) {
    case 'pages':
      pages($message);
      break;
    case 'page':
      page($message);
      break;
    case 'themes':
      themes($message);
      break;
    case 'plugins':
      plugins($message);
      break;
    case 'modules':
      modules($message);
      break;
    case 'theme':
      theme($message);
      break;
    case 'pagecreate':
      pagecreate($message);
      break;
    case 'instances':
      instances($message);
      break;
    case 'instance':
      instance($message);
      break;
    case 'profile':
      profile($message);
      break;
    case 'profile_user':
      profile_user($message);
      break;
    case 'categories':
      categories($message);
      break;
    case 'category':
      category($message);
      break;
    case 'products':
      products($message);
      break;
    case 'product':
      product($message);
      break;
    case 'customers':
      customers($message);
      break;
    case 'customer':
      customer($message);
      break;
    case 'payments':
      payments($message);
      break;
    case 'payment':
      payment($message);
      break;
    case 'shippings':
      shippings($message);
      break;
    case 'shipping':
      shipping($message);
      break;
    case 'orders':
      orders($message);
      break;
    case 'order':
      order($message);
      break;
    case 'parcels':
      parcels($message);
      break;
    case 'parcel':
      parcel($message);
      break;
    case 'transactions':
      transactions($message);
      break;
    case 'transaction':
      transaction($message);
      break;
    case 'customm':
      customm($message);
      break;
    default:
      //header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function pages($message) {
  global $smarty;
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);
  $smarty->assign('msg', json_encode($message));
}

function pagecreate($message) {
  global $smarty;
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);
  $smarty->assign('msg', json_encode($message));
}

function page($message) {
  global $smarty;
  $smarty->assign('md', MiscUtils::getParam('md', 0)); 
  
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $smarty->assign('menu_id', $id); 
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $smarty->assign('pages', DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d);
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $smarty->assign('menus', DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d);
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $smarty->assign('plugins', DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d);
    $opt = new stdClass();
    $opt->select_expr = 't.*, t1.name, t1.folder';
    $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
    $smarty->assign('instances', DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d);
    
    $smarty->assign('m', MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL));
  }
}

function themes($message) {
  global $smarty;
  $smarty->assign('themes', DbUtils::get(DbUtils::createPdoInst(), 'pz_theme', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d);
}

function plugins($message) {
  global $smarty;
  $smarty->assign('plugins', DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d);
}

function modules($message) {
  global $smarty;
  $smarty->assign('modules', DbUtils::get(DbUtils::createPdoInst(), 'pz_module', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d);
}

function theme($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', null);
  $smarty->assign('id', $id);
}

function instances($message) {
  global $smarty;
  $menu_id = MiscUtils::getParam('m', null);
  $smarty->assign('menu_id', $menu_id);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.menu_id = :v1';
  $cond_vals->v = array(':v1' => $menu_id);
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.name, t1.folder';
  $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
  $smarty->assign('instances', DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, 't.id', 'ASC', NULL, NULL, $opt)->d);
}

function instance($message) {
  global $smarty;
  $instance_id = MiscUtils::getParam('i', null);
  $plugin_folder = MiscUtils::getParam('f', null);
  
  $smarty->assign('instance_id', $instance_id);
  $smarty->assign('plugin_folder', $plugin_folder);
}

function profile($message) {
  global $smarty;
  $smarty->assign('profiles', DbUtils::get(DbUtils::createPdoInst(), 'pz_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d);
  $smarty->assign('users', DbUtils::get(DbUtils::createPdoInst(), 'pz_profile_user', NULL, NULL, NULL, NULL, NULL, NULL)->d);
}

function profile_user($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id= :v1';
    $cond_vals->v = array(':v1' => $id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_profile_user', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('users', $objects);
    }
  } else {
    $smarty->assign('users', array());
  }
}

function categories($message) {
  global $smarty;
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_category', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);
  $smarty->assign('msg', json_encode($message));
}

function category($message) {
  global $smarty;
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_category', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);
  $smarty->assign('msg', json_encode($message));
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id= :v1';
    $cond_vals->v = array(':v1' => $id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_category', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('category', $objects[0]);
    } else {
      $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_category', NULL, 't.order', 'ASC', 1, 1, NULL)->d;
      if (count($objects) == 1) {
        $smarty->assign('min_order', $objects[0]->order);
      } else {
        $smarty->assign('min_order', 1);
      }
    }
  }
}

function products($message) {
  global $smarty;
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.name AS category';
  $opt->join_expr = 'LEFT JOIN pz_ecom_category AS t1 ON t1.id = t.category_id';
  $smarty->assign('products', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product', NULL, NULL, NULL, NULL, NULL, $opt)->d);
}

function product($message) {
  global $smarty;
  $track_id = MiscUtils::getParam('pti', NULL);
  
  if ($track_id) {
    $smarty->assign('track_id', $track_id);
  } else {
    $smarty->assign('track_id', MiscUtils::unique());
  }
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.track_id= :v1';
  $cond_vals->v = array(':v1' => $track_id);
  $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($objects) == 1) {
    $smarty->assign('product', $objects[0]);
  } else {
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product', NULL, 't.order', 'ASC', 1, 1, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('min_order', $objects[0]->order);
    } else {
      $smarty->assign('min_order', 1);
    }
  }
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $track_id);
  $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_image', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  $smarty->assign('images', $objects);
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.product_track_id = :v1';
  $cond_vals->v = array(':v1' => $track_id);
  $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_attribute', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  $smarty->assign('attributes', $objects);
  
  $smarty->assign('categories', MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_category', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL));
}

function customers($message) {
  global $smarty;
  $p = MiscUtils::getParam('p', 1);
  $s = MiscUtils::getParam('s', SIZE);
  $o = MiscUtils::getParam('o', 'id');
  $q = MiscUtils::getParam('q', 'ASC');
  $cond = MiscUtils::getParam('con', NULL);
  
  if ($cond) {
    $cond_vals = new stdClass();
    $cond_vals->c = "t.email like '%$cond%' OR t.name like '%$cond%' OR t.tel like '%$cond%' OR t.mob like '%$cond%'";
    $cond_vals->v = array();
  } else {
    $cond_vals = NULL;
  }
  
  $result = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer', $cond_vals, $o, $q, $p, (int)$s, NULL);
  $smarty->assign('result', $result);
}

function customer($message) {
  global $smarty;
  $track_id = MiscUtils::getParam('cti', NULL);
  
  if ($track_id) {
    $smarty->assign('track_id', $track_id);
    $cond_vals = new stdClass();
    $cond_vals->c = 't.track_id= :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('customer', $objects[0]);
    }
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_track_id = :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer_image', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    $smarty->assign('images', $objects);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_track_id = :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer_address', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    $smarty->assign('addresses', $objects);
  } else {
    
    $smarty->assign('track_id', MiscUtils::unique());
    $smarty->assign('images', array());
    $smarty->assign('addresses', array());
  }
}

function payments($message) {
  global $smarty;
  $smarty->assign('payments', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_payment', NULL, NULL, NULL, NULL, NULL, NULL)->d);
}

function payment($message) {
  global $smarty;
  $track_id = MiscUtils::getParam('pti', NULL);
  
  if ($track_id) {
    $smarty->assign('track_id', $track_id);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.track_id = :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_payment', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('payment', $objects[0]);
    } else {
      $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_payment', NULL, 't.order', 'ASC', 1, 1, NULL)->d;
      if (count($objects) == 1) {
        $smarty->assign('min_order', $objects[0]->order);
      } else {
        $smarty->assign('min_order', 1);
      }
    }
  } else {
    $smarty->assign('track_id', MiscUtils::unique());
  }
}

function shippings($message) {
  global $smarty;
  $smarty->assign('shippings', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_shipping', NULL, NULL, NULL, NULL, NULL, NULL)->d);
}

function shipping($message) {
  global $smarty;
  $track_id = MiscUtils::getParam('pti', NULL);
  
  if ($track_id) {
    $smarty->assign('track_id', $track_id);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.track_id = :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_shipping', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $smarty->assign('shipping', $objects[0]);
    } else {
      $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_shipping', NULL, 't.order', 'ASC', 1, 1, NULL)->d;
      if (count($objects) == 1) {
        $smarty->assign('min_order', $objects[0]->order);
      } else {
        $smarty->assign('min_order', 1);
      }
    }
  } else {
    $smarty->assign('track_id', MiscUtils::unique());
  }
}

function orders($message) {
  global $smarty;
  $p = MiscUtils::getParam('p', 1);
  $s = MiscUtils::getParam('s', SIZE);
  $o = MiscUtils::getParam('o', 'id');
  $q = MiscUtils::getParam('q', 'ASC');
  $cond = MiscUtils::getParam('con', NULL);
  
  if ($cond) {
    $cond_vals = new stdClass();
    $cond_vals->c = "t.order_no like '%$cond%' OR t1.email like '%$cond%' OR t1.name like '%$cond%' OR t.total like '%$cond%' OR t.payment_name like '%$cond%' OR t.ship_name like '%$cond%'";
    $cond_vals->v = array();
  } else {
    $cond_vals = NULL;
  }
  
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.email AS customerEmail, t1.name As customerName';
  $opt->join_expr = 'LEFT JOIN pz_ecom_customer AS t1 ON t1.id = t.customer_id';
  
  $result = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, $o, $q, $p, (int)$s, $opt);
  $smarty->assign('result', $result);
}

function order($message) {
  global $smarty;
  $track_id = MiscUtils::getParam('oti', NULL);
  
  if ($track_id) {
    $smarty->assign('track_id', $track_id);
    $cond_vals = new stdClass();
    $cond_vals->c = 't.track_id = :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($objects) == 1) {
      $order = $objects[0];
    }
    $order->purchases = array();
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.order_id = :v1';
    $cond_vals->v = array(':v1' => $order->id);
    $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_purchase', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    foreach ($objects as &$pur) {
      $pur->attributes = array();
      $pur->product = new stdClass();
      $cond_vals = new stdClass();
      $cond_vals->c = 'id= :v1';
      $cond_vals->v = array(':v1' => $pur->product_id);
      $pur->product = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0];
      $pur->product->attributes = array();
      $cond_vals = new stdClass();
      $cond_vals->c = 'product_track_id = :v1';
      $cond_vals->v = array(':v1' => $pur->product->track_id);
      $attrs = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_attribute', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      foreach ($attrs as &$att) {
        $att->options = array();
        $cond_vals = new stdClass();
        $cond_vals->c = 'attribute_id= :v1';
        $cond_vals->v = array(':v1' => $att->id);
        $opts = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_option', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
        foreach ($opts as &$op) {
          $att->options[] = $op;
        }
        $pur->product->attributes[] = $att;
      }
      
      $cond_vals = new stdClass();
      $cond_vals->c = 'purchase_track_id = :v1';
      $cond_vals->v = array(':v1' => $pur->track_id);
      $attributes = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_purchase_attribute', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      foreach ($attributes as &$attr) {
        $attr->options = array();
        $cond_vals = new stdClass();
        $cond_vals->c = 'attribute_track_id = :v1';
        $cond_vals->v = array(':v1' => $attr->track_id);
        $options = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_purchase_option', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
        foreach ($options as &$opt) {
          $attr->options[] = $opt;
        }
        $pur->attributes[] = $attr;
      }
      $order->purchases[] = $pur;
    }
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $order->customer_id);
    $customers = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (isset($customers) && count($customers) > 0) {
      $customer = $customers[0];
      $cond_vals = new stdClass();
      $cond_vals->c = 't.customer_track_id= :v1';
      $cond_vals->v = array(':v1' => $customer->track_id);
      $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_customer_address', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      $smarty->assign('addresses', $objects);
    }
    
    $payments = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_payment', NULL, NULL, NULL, NULL, NULL, NULL)->d;
    
    $shippings = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_shipping', NULL, NULL, NULL, NULL, NULL, NULL)->d;
    
    $products = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product', NULL, NULL, NULL, NULL, NULL, NULL)->d;
    foreach ($products as &$product) {
      $product->attributes = array();
      $cond_vals = new stdClass();
      $cond_vals->c = 't.product_track_id = :v1';
      $cond_vals->v = array(':v1' => $product->track_id);
      $objects = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_attribute', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      foreach ($objects as &$attr) {
        $attr->options = array();
        $cond_vals = new stdClass();
        $cond_vals->c = 't.attribute_id = :v1';
        $cond_vals->v = array(':v1' => $attr->id);
        $attr->options = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_product_option', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
        $product->attributes[] = $attr;
      }
    }
    
    $smarty->assign('order', $order);
    $smarty->assign('payments', $payments);
    $smarty->assign('shippings', $shippings);
    $smarty->assign('products', $products);
  } else {
    $smarty->assign('track_id', MiscUtils::unique());
    $smarty->assign('addresses', array());
  }
}

function parcels($message) {
  global $smarty;
  $p = MiscUtils::getParam('p', 1);
  $s = MiscUtils::getParam('s', SIZE);
  $o = MiscUtils::getParam('o', 'id');
  $q = MiscUtils::getParam('q', 'ASC');
  $cond = MiscUtils::getParam('con', NULL);
  
  if ($cond) {
    $cond_vals = new stdClass();
    $cond_vals->c = "t1.order_no like '%$cond%' OR t.ship_name like '%$cond%' OR t.weight like '%$cond%' OR t.track_no like '%$cond%'";
    $cond_vals->v = array();
  } else {
    $cond_vals = NULL;
  }
  
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.order_no as order_no';
  $opt->join_expr = 'LEFT JOIN pz_ecom_order AS t1 ON t1.parcel_id = t.id';
  
  $smarty->assign('result', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_parcel', $cond_vals, $o, $q, $p, (int)$s, $opt));
}

function parcel($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', NULL);
  $oid = MiscUtils::getParam('oid', NULL);
  
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_parcel', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    $smarty->assign('parcels', $tmp);
    if (count($tmp) > 0) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.parcel_id = :v1';
      $cond_vals->v = array(':v1' => $tmp[0]->id);
      $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($tmp) > 0) {
        $oid = $tmp[0]->id;
      }
    }
  }
  
  if ($oid) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $oid);
    $smarty->assign('orders', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.order_id = :v1';
    $cond_vals->v = array(':v1' => $oid);
    $smarty->assign('purchases', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_purchase', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d);
    
  } else {
    $smarty->assign('orders', array());
    $smarty->assign('purchases', array());
  }
  
  $smarty->assign('shippings', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_mthd_shipping', NULL, NULL, NULL, NULL, NULL, NULL)->d);
}


function transactions($message) {
  global $smarty;
  $p = MiscUtils::getParam('p', 1);
  $s = MiscUtils::getParam('s', SIZE);
  $o = MiscUtils::getParam('o', 'id');
  $q = MiscUtils::getParam('q', 'ASC');
  $cond = MiscUtils::getParam('con', NULL);
  
  if ($cond) {
    $cond_vals = new stdClass();
    $cond_vals->c = "t2.order_no like '%$cond%' OR t1.email like '%$cond%' OR t1.name like '%$cond%' OR t.total like '%$cond%' OR t.payment_name like '%$cond%'";
    $cond_vals->v = array();
  } else {
    $cond_vals = NULL;
  }
  
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.email AS customerEmail, t1.name As customerName, t2.order_no as orderNo';
  $opt->join_expr = 'LEFT JOIN pz_ecom_customer AS t1 ON t1.id = t.customer_id LEFT JOIN pz_ecom_order AS t2 ON t2.transaction_id = t.id';
  
  $result = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_transaction', $cond_vals, $o, $q, $p, (int)$s, $opt);
  $smarty->assign('result', $result);
}

function transaction($message) {
  global $smarty;
  
  $track_id = MiscUtils::getParam('tti', NULL);
  $oid = MiscUtils::getParam('oid', NULL);
  
  if ($track_id) {
//    $smarty->assign('track_id', $track_id);
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.track_id= :v1';
    $cond_vals->v = array(':v1' => $track_id);
    $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_transaction', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    $smarty->assign('transactions', $tmp);
    if (count($tmp) > 0) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.transaction_id = :v1';
      $cond_vals->v = array(':v1' => $tmp[0]->id);
      $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
      if (count($tmp) > 0) {
        $oid = $tmp[0]->id;
      }
    }
  }
    
  if ($oid) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $oid);
    $smarty->assign('orders', DbUtils::get(DbUtils::createPdoInst(), 'pz_ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d);
  } else {
    $smarty->assign('orders', array());
  }
}

function customm($message) {
  global $smarty;
  $smarty->assign('query', $_SERVER['QUERY_STRING']);
}
?>